package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 442.数组中重复的数据
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2022/5/8 19:51
 */
public class P442FindAllDuplicatesInAnArray {
    public static void main(String[] args) {
        Solution solution = new P442FindAllDuplicatesInAnArray().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<Integer> findDuplicates(int[] nums) {
            Set<Integer> set = new HashSet<>();

            List<Integer> result = new ArrayList<>();
            for (int num : nums) {
                if (set.contains(num)) {
                    result.add(num);
                } else {
                    set.add(num);
                }
            }

            return result;
        }
    }
}
